package com.jshx.sz.gdjt.thread;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.jshx.sz.gdjt.thread.data.DataProUtil;
import com.jshx.sz.gdjt.util.AccessDBImp;
import com.jshx.sz.gdjt.util.TDESEncodeUtils;
import com.jshx.sz.gdjt.util.file.UploadItem;
import com.jshx.sz.gdjt.util.file.UploadMultiFileThread;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;

import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
 

public class CommitDataThread  extends Thread{
	private HashMap  dataMap = new HashMap ();
	private String type;
	private String dataNumber;
	private String assetCode;
	
	
	public CommitDataThread()
	{
		
	}
	/**
	 * 
	 * @param handler 回调
	 * @param params 参数
	 * @param dataMap 数据集合
	 * @param type 类型  check 待检查   ， examine 待审核 ， checkHis 检查历史 ， examineHis 审核历史 
	 * @param dataNumber 工艺单编号
	 * @param assetCode 设备编号
	 */
	public CommitDataThread(   HashMap  dataMap,String type,String dataNumber,String assetCode)
	{
		this.dataMap=dataMap;
		this.assetCode=assetCode;
		this.type=type;
		this.dataNumber=dataNumber;
	}
	
	public void run() 
	{
		try {
			String orderNum=dataMap.get("order_num")+"";
			// 1 检查ADD 2 检查 UPDATE 3 审核 ADD 4 审核UPDATE
			if(type.equals("check")) { type="1"; }
			else if(type.equals("examine")) { type="3"; }
			else if(type.equals("checkHis")) { type="2"; }
			else if(type.equals("examineHis")) { type="4"; }
			
			String params = ",'dataNumber':'" + dataNumber + "','orderNo':'" + orderNum + 
			"','userId':'" + ApplicationGlobal.userId + "','assetCode':'" + assetCode + "','type':'"+type+"'";
			String proRe=DataProUtil.LocalMapToServerString(dataMap, dataNumber);
			params+=proRe;
			
			AccessDBImp dao= new AccessDBImp();
			String re=dao.proDB("updateData", params);
		 
			
			 
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
			Message msg=new Message();
			 
		}  
	}
	 
	
	/**
	 * 图片上传到服务器
	 */
	public void imgToServer(UploadMultiFileThread uploadMultiThread ,List<String> imgNameList)
	{
		
		for(int i=0;i<imgNameList.size();i++)
		{
			 // /storage/emulated/0/SZGDJT/imgs/  20130901194959.jpg   ,
			String filePath=imgNameList.get(i);
			String saveName=filePath.substring(filePath.lastIndexOf("/")+1);
		 
			
			String url = String.format(
					"%1$s?photoName=%2$s",
					ApplicationGlobal.uploadImageUrl,
					saveName,imgNameList.get(i),
					""
					);		
			UploadItem item = new UploadItem();
			item.setUrl(url);
			Log.d("Check...URL", url);
			item.setPath(imgNameList.get(i));
			item.setTag1(String.valueOf(i+1));
			
			if(i==imgNameList.size()-1)
			{
				item.setTag2("last");
			}
			uploadMultiThread.add(item);
			//ProgressDialogUtil.show(this, "图片上传中", false);
		}
	}
	
	
 
	 
}
